<template>
    <div class="app-container">
        <el-card shadow="never">    
            <!-- 条件查询 -->
            <!-- <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                <el-row>

                    <el-col :md="6" :sm="12">
                        <el-form-item class="ele-text-right" label-width="50px">
                            <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                            <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>

                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form> -->
            <p>当前操作    
                <span style="font-size: 20px;color:#fd2f2f;">{{start_time}}</span>  
                的成本数据。
                <!-- 汇率换算1 美元 ≈ 
                <span style="font-size: 20px;color:#fd2f2f;">7.150</span>
                    人民币 -->
                <!-- <el-button type="primary" size="mini" @click="modify_exchange_rate">修改当月汇率</el-button> -->
                <el-button type="primary" size="mini" @click="open">查看当月业绩规则</el-button>
            </p>
            <p>
                本月是否开启业绩平分规则
                <el-switch
                    v-model="switch_value"
                    active-color="#ff4949"
                    inactive-color="#13ce66"
                    active-text="关闭"
                    inactive-text="开启">
                </el-switch>
                <!-- <el-button class="btn-mar" size="mini" type="success" icon="el-icon-c-scale-to-original" @click="download_achievement()">下载平分业绩明细</el-button> -->
            </p>
            <el-row>
                <el-col :sm="12" :lg="6">
                    <el-result v-if="logistics_count >= 10" icon="error" title="物流成本" subTitle="">
                        <template slot="extra">
                            <p>等待匹配：<span style="color:#fd2f2f;">{{logistics_count}}</span>条/总条数{{logistics_countz}}</p>
                            <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-edit">
                                    <router-link
                                            :to="{
                                                path: 'cost/costlogistics_index',
                                            }"
                                        >
                                        填写
                                    </router-link>
                                </el-button>
                            <!-- <el-button type="primary" size="medium">更新</el-button> -->
                        </template>
                    </el-result>
                    <el-result v-if="logistics_count < 10" icon="success" title="物流成本" subTitle="">
                        <template slot="extra">
                            <p>等待匹配：<span style="color:#fd2f2f;">{{logistics_count}}</span>条/总条数{{logistics_countz}}</p>
                            <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-edit">
                                    <router-link
                                            :to="{
                                                path: 'cost/costlogistics_index',
                                            }"
                                        >
                                        填写
                                    </router-link>
                                </el-button>
                            <!-- <el-button type="primary" size="medium">更新</el-button> -->
                        </template>
                    </el-result>
                </el-col>
                <el-col :sm="12" :lg="6">
                    <el-result v-if="ismaterial_count >= 10" icon="error" title="采购成本" subTitle="">
                        <template slot="extra">
                            <p>等待匹配：<span style="color:#fd2f2f;">{{ismaterial_count}}</span>条/总条数{{ismaterial_countz}}</p>
                            <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-edit">
                                    <router-link
                                            :to="{
                                                path: 'cost/querypurchase_index',
                                            }"
                                        >
                                        填写
                                    </router-link>
                                </el-button>
                            <!-- <el-button type="primary" size="medium">更新</el-button> -->
                        </template>
                    </el-result>
                    <el-result v-if="ismaterial_count < 10" icon="success" title="采购成本" subTitle="">
                        <template slot="extra">
                            <p>等待匹配：<span style="color:#fd2f2f;">{{ismaterial_count}}</span>条/总条数{{ismaterial_countz}}</p>
                            <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-edit">
                                    <router-link
                                            :to="{
                                                path: 'cost/querypurchase_index',
                                            }"
                                        >
                                        填写
                                    </router-link>
                                </el-button>
                            <!-- <el-button type="primary" size="medium">更新</el-button> -->
                        </template>
                    </el-result>
                </el-col>
                <el-col :sm="12" :lg="6">
                    <el-result v-if="dispute_count >= 10" icon="error" title="争议订单" subTitle="">
                        <template slot="extra">
                            <p>等待匹配：<span style="color:#fd2f2f;">{{dispute_count}}</span>条/总条数{{dispute_countz}}</p>
                            <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-edit">
                                    <router-link
                                            :to="{
                                                path: 'cost/costdispute_index',
                                            }"
                                        >
                                        填写
                                    </router-link>
                                </el-button>
                        </template>
                    </el-result>
                    <el-result v-if="dispute_count < 10" icon="success" title="争议订单" subTitle="">
                        <template slot="extra">
                            <p>等待匹配：<span style="color:#fd2f2f;">{{dispute_count}}</span>条/总条数{{dispute_countz}}</p>
                            <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-edit">
                                    <router-link
                                            :to="{
                                                path: 'cost/costdispute_index',
                                            }"
                                        >
                                        填写
                                    </router-link>
                                </el-button>
                        </template>
                    </el-result>
                </el-col>
                <el-col :sm="12" :lg="6">
                    <el-result icon="success" title="广告以及归属标签" subTitle="">
                    <template slot="extra">
                        <el-button class="btn-mar" size="mini" type="primary" icon="el-icon-edit">
                                    <router-link
                                            :to="{
                                                path: '/site/site_log_index',
                                            }"
                                        >
                                        查看
                                    </router-link>
                                </el-button>
                    </template>
                    </el-result>
                </el-col>
            </el-row>

            <!-- <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="open" v-hasPermi="api.add">添加</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="api.delete">删除</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" v-loading="exportLoading" @click="handleExport" v-hasPermi="api.export">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-row> -->
            <el-divider></el-divider>
            <el-divider></el-divider>
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                <el-row>
                    <el-col :md="12" :sm="12">
                        <el-form-item label="月份:" prop="month.value">
                            <el-date-picker
                                v-model="queryParams.month.value"
                                type="month"
                                placeholder="选择月份">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :md="12" :sm="12">
                        <el-form-item label="主运营:" prop="admin_id.value">
                            <SelectPage v-model="queryParams.admin_id.value" :url="'/admin/system.admin/selectPage'" show_field="nickname" query_field="nickname" :multiple="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :md="12" :sm="12">
                        <el-form-item label="团队:" prop="team_id.value">
                            <el-input v-model="queryParams.team_id.value" disabled/>
                        </el-form-item>
                    </el-col>
                    <el-col :md="12" :sm="12">
                        <el-form-item class="ele-text-right" label-width="50px">
                            <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                            <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>

                        </el-form-item>
                    </el-col>
                </el-row>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-form>

            <el-descriptions title="统计">
                <el-descriptions-item label="个人业绩$">{{yztotal_price}}</el-descriptions-item>
                <el-descriptions-item label="已发货销售额$">{{ztotal_price}}</el-descriptions-item>
                <el-descriptions-item label="总盈利$">{{zprofit}}</el-descriptions-item>
            </el-descriptions>

            <!-- 列表 -->

            <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
                <el-tab-pane label="所有">
                        <qa-table
                            :source="tableData"
                            :columns="columns"
                            :selection="true"
                            :loading="loading"
                            @sortChange="sortChange"
                            @statusChange="statusChange"
                            @selectionChange="selectionChange"
                            id="quickadmin"
                        >
                            <template v-slot:action>
                                <el-table-column label="操作" width="" align="center">
                                    <template slot-scope="scope">
                                        <el-button class="btn-mar" size="mini" type="primary" @click="handleEdit(scope.row)" v-hasPermi="api.edit">修改总广告费</el-button>
                                        <!-- <el-button class="btn-mar" size="mini" type="primary" @click="handleEdit(scope.row)" v-hasPermi="api.edit">查看每日业绩</el-button>m -->
                                        <el-button class="btn-mar" size="mini" type="success" @click="download_sales(scope.row)">下载业绩明细</el-button>
                                        <!-- <el-button class="btn-mar" size="mini" type="warning" icon="el-icon-tickets" @click="handleConfirmShipments(scope.row)">更新业绩</el-button> -->
                                        <el-button class="btn-mar" size="mini" type="warning" @click="archive_performance(scope.row)">归档业绩</el-button>
                                    </template>
                                </el-table-column>
                            </template>
                        </qa-table>

                        <!-- 分页 -->
                        <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
                </el-tab-pane>
                <el-tab-pane label="武汉">
                        <qa-table
                            :source="tableData"
                            :columns="columns"
                            :selection="true"
                            :loading="loading"
                            @sortChange="sortChange"
                            @statusChange="statusChange"
                            @selectionChange="selectionChange"
                            id="quickadmin"
                        >
                            <template v-slot:action>
                                <el-table-column label="操作" width="" align="center">
                                    <template slot-scope="scope">
                                        <el-button class="btn-mar" size="mini" type="primary" @click="handleEdit(scope.row)" v-hasPermi="api.edit">修改总广告费</el-button>
                                        <!-- <el-button class="btn-mar" size="mini" type="primary" @click="handleEdit(scope.row)" v-hasPermi="api.edit">查看每日业绩</el-button>m -->
                                        <el-button class="btn-mar" size="mini" type="success" @click="download_sales(scope.row)">下载业绩明细</el-button>
                                        <!-- <el-button class="btn-mar" size="mini" type="warning" icon="el-icon-tickets" @click="handleConfirmShipments(scope.row)">更新业绩</el-button> -->
                                        <el-button class="btn-mar" size="mini" type="warning" @click="archive_performance(scope.row)">归档业绩</el-button>
                                    </template>
                                </el-table-column>
                            </template>
                        </qa-table>

                        <!-- 分页 -->
                        <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
                    
                </el-tab-pane>
                <el-tab-pane label="南昌">
                        <qa-table
                            :source="tableData"
                            :columns="columns"
                            :selection="true"
                            :loading="loading"
                            @sortChange="sortChange"
                            @statusChange="statusChange"
                            @selectionChange="selectionChange"
                            id="quickadmin"
                        >
                            <template v-slot:action>
                                <el-table-column label="操作" width="" align="center">
                                    <template slot-scope="scope">
                                        <el-button class="btn-mar" size="mini" type="primary" @click="handleEdit(scope.row)" v-hasPermi="api.edit">修改总广告费</el-button>
                                        <!-- <el-button class="btn-mar" size="mini" type="primary" @click="handleEdit(scope.row)" v-hasPermi="api.edit">查看每日业绩</el-button>m -->
                                        <el-button class="btn-mar" size="mini" type="success" @click="download_sales(scope.row)">下载业绩明细</el-button>
                                        <!-- <el-button class="btn-mar" size="mini" type="warning" icon="el-icon-tickets" @click="handleConfirmShipments(scope.row)">更新业绩</el-button> -->
                                        <el-button class="btn-mar" size="mini" type="warning" @click="archive_performance(scope.row)">归档业绩</el-button>
                                    </template>
                                </el-table-column>
                            </template>
                        </qa-table>

                        <!-- 分页 -->
                        <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
                    
                </el-tab-pane>
                <el-tab-pane label="银川">
                        <qa-table
                            :source="tableData"
                            :columns="columns"
                            :selection="true"
                            :loading="loading"
                            @sortChange="sortChange"
                            @statusChange="statusChange"
                            @selectionChange="selectionChange"
                            id="quickadmin"
                        >
                            <template v-slot:action>
                                <el-table-column label="操作" width="" align="center">
                                    <template slot-scope="scope">
                                        <el-button class="btn-mar" size="mini" type="primary" @click="handleEdit(scope.row)" v-hasPermi="api.edit">修改总广告费</el-button>
                                        <!-- <el-button class="btn-mar" size="mini" type="primary" @click="handleEdit(scope.row)" v-hasPermi="api.edit">查看每日业绩</el-button>m -->
                                        <el-button class="btn-mar" size="mini" type="success" @click="download_sales(scope.row)">下载业绩明细</el-button>
                                        <!-- <el-button class="btn-mar" size="mini" type="warning" icon="el-icon-tickets" @click="handleConfirmShipments(scope.row)">更新业绩</el-button> -->
                                        <el-button class="btn-mar" size="mini" type="warning" @click="archive_performance(scope.row)">归档业绩</el-button>
                                    </template>
                                </el-table-column>
                            </template>
                        </qa-table>

                        <!-- 分页 -->
                        <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
                    
                </el-tab-pane>
            </el-tabs>
        </el-card>

        <!-- 编辑弹窗 -->
       <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editOpen"  :before-close="beforeClose" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData"  :disabled="disabled" @submitForm="submitForm" :editFormParams="editFormParams" @editFormCancel="beforeClose" />
        </el-dialog>

    </div>
</template>

<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormCom'
export default {
    name: 'Curd',
    mixins: [curdMixin],
    components: { EditForm },
    
    data() {
        return {
            switch_value: true,
            // table结构
            columns: [
            {
                    visible: true,
                    label: '统计月份',
                    prop: 'timedate',
                },
                {
                    visible: true,
                    label: '运营',
                    prop: 'admin_id',
                },
                {
                    visible: true,
                    label: '个人业绩$',
                    prop: 'ytotal_price',
                },
                {
                    visible: true,
                    label: '已发货销售额$',
                    prop: 'total_price',
                },
                {
                    visible: true,
                    label: '94%销售额$',
                    prop: 'sales_markup',
                },
                {
                    visible: true,
                    label: '总物流$',
                    prop: 'freight',
                },
                {
                    visible: true,
                    label: '总采购$',
                    prop: 'material',
                },
                {
                    visible: true,
                    label: '总物流加成$',
                    prop: 'logistics_markup',
                },
                {
                    visible: true,
                    label: '总广告$',
                    prop: 'advertising',
                },
                {
                    visible: true,
                    label: '争议费$',
                    prop: 'dispute_fees',
                },
                {
                    visible: true,
                    label: '平分销售额$',
                    prop: 'sales_revenue',
                },
                {
                    visible: true,
                    label: '最终盈利$',
                    prop: 'profit',
                },
                {
                    visible: true,
                    label: '归档状态',
                    prop: 'performance',
                },
            ],
            // 接口地址
            api: {
                index: `/admin/personreport/index`,
                find: `/admin/personreport/find`,
                add: `/admin/personreport/add`,
                edit: `/admin/personreport/edit`,
                delete: `/admin/personreport/delete`,
                export: `/admin/personreport/export_personreport_day`,
                person_report: `/admin/taskachievement/person_report`,
                person_report_performance: `/admin/taskachievement/person_report_performance`,
                performance_equally: `/admin/taskachievement/performance_equally`,
                status: `/admin/personreport/status`
            },
            editFormParams: {
            },
            pageInfo: {
                sort: 'id',
            },
            primaryKey: 'id',
            // 搜索表单是否展开
            searchExpand: false,
              //是否展示搜索
            showSearch: true,
            // 查询参数
            queryParams: {
                month: { value:undefined, op: '='},
                number: { value:undefined, op: 'like'},
                admin_id: { value: [], op: '=' },
                team_id: { value: [], op: '=' },
            },
            activeName: 0,
            activeName2: '所有'
        }
    },
    created () {
        this.initIndex()
    },
    watch: {
        
    },
    methods: {
        handleClick(tab, event) {
            switch (this.activeName) {
                case '0':
                    this.queryParams.team_id.value = '所有'
                    break
                case '1':
                    this.queryParams.team_id.value = '武汉'
                    break
                case '2':
                    this.queryParams.team_id.value = '南昌'
                    break
                case '3':
                    this.queryParams.team_id.value = '银川'
                    break
                default:
                    this.queryParams.team_id.value = '所有'
            }
            this.submitSearch();
        },
        // 案列：当前组件格式化方法
        testFormatter(prop) {
            return prop
        },
        modify_exchange_rate(row) {
            this.$confirm(`修改数据请等待一分钟，确定要修改吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.$message.success('修改成功');
                })
                .catch(err => {})
        },
        divide_performance_equally() {
            this.$confirm(`更新数据请等待一分钟，确定要更新吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.baseRequest_equally({ id: 1})
                })
                .catch(err => {})
        },
        baseRequest_equally({ id }) {
            this.loading = true
            this.request
                .post(this.api.performance_equally, { id })
                .then(response => {
                    if(response == 1){
                        this.$message.success('更新成功');
                    }
                    if(response == 2){
                        this.$message.error('网络错误，请刷新重试');
                    }
                    this.initIndex();
                })
                .finally(() => {
                    this.loading = false
                })
        },
        handleConfirmShipments(row) {
            this.$confirm(`更新数据请等待一分钟，确定要更新吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.baseRequest({ id: row.id})
                })
                .catch(err => {})
        },
        baseRequest({ id }) {
            this.loading = true
            this.request
                .post(this.api.person_report, { id })
                .then(response => {
                    if(response == 1){
                        this.$message.success('更新成功');
                    }
                    if(response == 2){
                        this.$message.error('网络错误，请刷新重试');
                    }
                    this.initIndex();
                })
                .finally(() => {
                    this.loading = false
                })
        },
        archive_performance(row) {
            this.$confirm(`确定要归档吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.baseRequest_performance({ id: row.id})
                    this.initIndex();
                })
                .catch(err => {})
        },
        baseRequest_performance({ id }) {
            this.loading = true
            this.request
                .post(this.api.person_report_performance, { id })
                .then(response => {
                    if(response == 1){
                        this.$message.success('归档成功');
                    }
                    if(response == 2){
                        this.$message.error('网络错误，请刷新重试');
                    }
                    if(response == 3){
                        this.$message.error('已归档');
                    }
                    this.initIndex();
                })
                .finally(() => {
                    this.loading = false
                })
        },
        download_sales(row) {  
            const qyparams = {
                id: row.id,
            }
            this.$confirm(`确定要下载订单明细吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {  
                    this.loading = true
                    return this.request.get('/admin/personreport/download_sales', { params: qyparams, responseType: 'blob' })
                })
                .then(response => {
                    this.download(response, row.timedate+''+row.admin_id+'订单明细.xlsx')
                    this.$message.success('下载成功');
                    this.initIndex();
                })
                .catch(err => {})
                .finally(() => {
                    this.loading = false
                })
        },
        download_achievement() {
            this.loading = true
            const qyparams = {
                id: 1,
            }
            this.$confirm(`确定要下载平分业绩明细吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    return this.request.get('/admin/personreport/download_achievement', { params: qyparams, responseType: 'blob' })
                })
                .then(response => {
                    this.download(response,'平分业绩明细.xlsx')
                    this.$message.success('下载成功');
                    this.initIndex();
                })
                .catch(err => {})
                .finally(() => {
                    this.loading = false
                })
        },
        open() {
            // this.$alert('<strong>这是 <i>HTML</i> 片段</strong>', '业绩规则', {
            this.$alert('<strong>1、<i>已付款(未发货)</i>   不算业绩，但是下个月如果发货则会算上</strong><br/><strong>2、<i>已退款(未发货)</i>  不算业绩</strong><br/><strong>3、<i>已退款(已发货)</i>  不算销售额，但是要计算成本</strong><br/><strong>4、<i>争议订单</i>  不算销售额，但是要计算成本</strong><br/><strong>5、<i>物流加成</i>  是指发货的每个订单两块钱</strong><br/><strong>6、如果一条订单存在两个运营的标签，则<i>平分业绩</i> </strong><br/><strong>7、新品老品分开计算业绩，新品标签是<i>FBNEW</i>，区分大小写 </strong>', '业绩规则', {
                dangerouslyUseHTMLString: true
            });
        }
    }
}
</script>
<style lang="scss">
.btn-mar {
    margin: 5px !important;
}
</style>
